package com.gel.codegeneration.controller;

import cn.hutool.core.bean.BeanUtil;
import com.gel.codegeneration.common.Annotation.AuthCheck;
import com.gel.codegeneration.common.constant.UserConstant;
import com.gel.codegeneration.common.enums.ErrorCode;
import com.gel.codegeneration.convention.exception.BusinessException;

import com.gel.codegeneration.convention.result.BaseResponse;
import com.gel.codegeneration.convention.result.ResultUtils;
import com.gel.codegeneration.model.dto.req.user.*;
import com.gel.codegeneration.model.dto.resp.user.LoginUserVO;
import com.gel.codegeneration.model.dto.resp.user.UserVO;
import com.gel.codegeneration.model.entity.User;
import com.gel.codegeneration.service.UserService;
import com.gel.codegeneration.utils.ThrowUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 用户 控制层。
 *
 * @author <a>gel</a>
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * @description:用户注册
     * @author: gel
     * @param: [userRegisterRequest]
     * @return: com.gel.codegeneration.convention.result.BaseResponse<java.lang.Long>
     **/
    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest userRegisterRequest){
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        long userId = userService.register(userRegisterRequest.getUserAccount(), userRegisterRequest.getUserPassword(), userRegisterRequest.getCheckPassword());
        return ResultUtils.success(userId);

    }
    /**
     * @description: 用户登录
     * @author: gel
     * @param: [userLoginRequest, request]
     * @return: com.gel.codegeneration.convention.result.BaseResponse<com.gel.codegeneration.model.dto.resp.user.LoginUserVO>
     **/
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.login(userLoginRequest.getUserAccount(), userLoginRequest.getUserPassword(), request));
    }
    /**
     * @description: 获取当前登录用户
     * @author: gel
     * @param: [request]
     * @return: com.gel.codegeneration.convention.result.BaseResponse<com.gel.codegeneration.model.dto.resp.user.LoginUserVO>
     **/
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(loginUser, loginUserVO);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * @description: 注销登录
     * @author: gel
     * @param: [request]
     * @return: com.gel.codegeneration.convention.result.BaseResponse<java.lang.Boolean>
     **/
    @PostMapping("/logout")
    public BaseResponse<Boolean> logout(HttpServletRequest request) {
        return  ResultUtils.success(userService.userLoginOut(request));
    }

    //新增用户
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> add(@RequestBody UserAddRequest request){
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        User user = BeanUtil.toBean(request, User.class);
        final String DEFAULT_PASSWORD="12345678";
        user.setUserPassword(userService.getEncryptedPassword(DEFAULT_PASSWORD));
        boolean result = userService.save(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }
    //根据id获取用户

    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id){
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }
    //根据id获取包装类
    @GetMapping("/getVO")
    public BaseResponse<UserVO> getUserVOByIdVO(long id){
        BaseResponse<User> userById = getUserById(id);
        User user= userById.getData();
        return ResultUtils.success(userService.getUseVO(user));

    }
    //删除用户
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest request){
        if(request==null||request.getId() <=0){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userService.removeById(request.getId());
        return ResultUtils.success(b);
    }
    //更新用户信息
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest  request){
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(request.getId() ==null, ErrorCode.PARAMS_ERROR);
        User user = BeanUtil.toBean(request, User.class);
        boolean result = userService.updateById(user);
        return ResultUtils.success(result);
    }
    //分页获取用户信息
    @GetMapping("/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> getUserPage(@RequestBody UserQueryRequest userQueryRequest){
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        QueryWrapper querywrapper = userService.getQuerywrapper(userQueryRequest);
        Page<User> page = userService.page(new Page<>(userQueryRequest.getPageNum(), userQueryRequest.getPageSize()), querywrapper);
        Page<UserVO> userVOPage = new Page<>(userQueryRequest.getPageNum(), userQueryRequest.getPageSize(), page.getTotalRow());
        List<UserVO> userVOList = userService.getUserVOList(page.getRecords());
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);

    }
}
